<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="退货单编号" prop="saleBackCode">
        <el-input
          v-model="queryParams.saleBackCode"
          placeholder="请输入退货单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退货申请单编号" prop="saleBackRequestCode">
        <el-input
          v-model="queryParams.saleBackRequestCode"
          placeholder="请输入退货申请单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-date-picker clearable
                        v-model="queryParams.createDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改人" prop="updateBy">
        <el-input
          v-model="queryParams.updateBy"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改时间" prop="updateDate">
        <el-date-picker clearable
                        v-model="queryParams.updateDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="showAdd"
          v-hasPermi="['xiaoshou:saleBack:add']"
        >新增
        </el-button>
      </el-col>
      <AddSaleBack @updList="getList" :updData="this.updData" ref="asb"></AddSaleBack>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xiaoshou:saleBack:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <SaleBackView :viewData="this.viewData" ref="sbv"></SaleBackView>
    <el-table v-loading="loading" :data="saleBackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="销售退货单编号" align="center" prop="saleBackCode" width="200px">
        <template slot-scope="scope">
          <el-button type="text" @click="showView(scope.row)">{{
              scope.row.saleBackCode
            }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="销售退货申请单编号" align="center" prop="saleBackRequestCode" width="200px"/>
      <el-table-column label="客户名称" align="center" prop="cusName"/>
      <el-table-column label="总价" align="center" prop="total"/>
      <el-table-column label="需付款比率" align="center" prop="payCent">
        <template slot-scope="scope">
          {{ scope.row.payCent * 100 + "%" }}
        </template>
      </el-table-column>
      <el-table-column label="需付款金额" align="center" prop="realMoney"/>
      <el-table-column label="实付金额" align="center" prop="payMoney"/>
      <el-table-column label="总数量" align="center" prop="totalNum"/>
      <el-table-column label="已入库数量" align="center" prop="inNum"/>
      <el-table-column label="不合格数量" align="center" prop="noPassNum"/>
      <el-table-column label="入库状态" align="center" prop="stockStatus">
        <template slot-scope="scope">
          {{ scope.row.stockStatus == 0 ? "等待入库" : scope.row.stockStatus == 2 ? "完全入库" : "部分入库" }}
        </template>
      </el-table-column>
      <el-table-column label="付款状态" align="center" prop="payStatus">
        <template slot-scope="scope">
          {{ scope.row.payStatus == 0 ? "等待付款" : scope.row.payStatus == 2 ? "完全付款" : "部分付款" }}
        </template>
      </el-table-column>
      <el-table-column label="质检状态" align="center" prop="qualitiedStatus">
        <template slot-scope="scope">
          {{ scope.row.qualitiedStatus == 0 ? "等待质检" : scope.row.qualitiedStatus == 1 ? "部分质检" : "质检完成" }}
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="passUser"/>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          {{ scope.row.status == 0 ? "待审核" : scope.row.status == 1 ? "审核通过" : "已驳回" }}
        </template>
      </el-table-column>
      <el-table-column label="生效时间" align="center" prop="passDate"/>
      <el-table-column label="是否关闭" align="center" prop="isClose">
        <template slot-scope="scope">
          {{ scope.row.isClose == 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column label="是否作废" align="center" prop="isCancel">
        <template slot-scope="scope">
          {{ scope.row.isCancel == 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateBy">
        <template slot-scope="scope">
          {{ scope.row.updateBy == '0' ? "" : scope.row.updateBy }}
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateDate" width="180">
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="120px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            :disabled="scope.row.status==1||scope.row.status==2||scope.row.isClose==1||scope.row.isCancel==1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCheck(scope.row)"
            v-hasPermi="['xiaoshou:saleBack:edit']"
          >审核
          </el-button>
          <el-button
            :disabled="scope.row.status==1||scope.row.status==2||scope.row.isClose==1||scope.row.isCancel==1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xiaoshou:saleBack:edit']"
          >修改
          </el-button>
          <el-button
            :disabled="scope.row.status==1||scope.row.status==2||scope.row.isClose==1||scope.row.isCancel==1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xiaoshou:saleBack:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listSaleBack, getSaleBack, delSaleBack, addSaleBack, updateSaleBack} from "@/api/xiaoshou/saleBack";
import SaleBackView from "@/views/xiaoshou/saleBack/SaleBackView.vue";
import AddSaleBack from "@/views/xiaoshou/saleBack/AddSaleBack.vue";
import request from "@/utils/request";

export default {
  name: "SaleBack",
  components: {SaleBackView, AddSaleBack},
  data() {
    return {
      updData: {},
      viewData: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售退货单表格数据
      saleBackList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        saleBackCode: null,
        saleBackRequestCode: null,
        total: null,
        payCent: null,
        realMoney: null,
        payMoney: null,
        stockStatus: null,
        payStatus: null,
        status: null,
        qualitiedStatus: null,
        createBy: null,
        createDate: null,
        updateBy: null,
        updateDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        saleBackCode: [
          {required: true, message: "退货单编号不能为空", trigger: "blur"}
        ],
        saleBackRequestCode: [
          {required: true, message: "退货申请单编号不能为空", trigger: "blur"}
        ],
        total: [
          {required: true, message: "总价不能为空", trigger: "change"}
        ],
        payCent: [
          {required: true, message: "需付款比率不能为空", trigger: "change"}
        ],
        realMoney: [
          {required: true, message: "需付款金额不能为空", trigger: "change"}
        ],
        payMoney: [
          {required: true, message: "实付金额不能为空", trigger: "change"}
        ],
        status: [
          {required: true, message: "审核状态不能为空", trigger: "change"}
        ],
        qualitiedStatus: [
          {required: true, message: "质检状态不能为空", trigger: "change"}
        ],
        createBy: [
          {required: true, message: "创建人不能为空", trigger: "blur"}
        ],
        createDate: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
      }
    };
  },

  created() {
    this.getList();
  },
  methods: {
    //审核
    handleCheck(row) {
      let saleBack = {};
      saleBack.id = row.id;
      saleBack.status = 1;
      saleBack.passUser = this.$store.getters.name;
      saleBack.passDate = new Date().getTime();
      request({
        url: '/xiaoshou/saleBack/checkSaleBack/',
        data: saleBack,
        method: "post"
      }).then(res => {
        if (res.code == 200) {
          let message = "";
          if (saleBack.status == 1) {
            message = "审核通过完成！";
          } else {
            message = "驳回完成！";
          }
          this.$notify({
            type: "success",
            message: message,
            title: "审核"
          })
          this.getList();
        } else {
          this.$notify({
            type: "error",
            message: "审核失败",
            title: "审核"
          })
        }

      });
    },
    showView(row) {
      this.viewData = row;
      this.$refs.sbv.showView();
    },
    showAdd() {
      this.$refs.asb.showAdd();
    },
    /** 查询销售退货单列表 */
    getList() {
      this.loading = true;
      listSaleBack(this.queryParams).then(response => {
        this.saleBackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        saleBackCode: null,
        saleBackRequestCode: null,
        total: null,
        payCent: null,
        realMoney: null,
        payMoney: null,
        stockStatus: null,
        payStatus: null,
        status: null,
        qualitiedStatus: null,
        createBy: null,
        createDate: null,
        updateBy: null,
        updateDate: null,
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销售退货单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSaleBack(id).then(response => {
        this.updData = response.data;
        this.$refs.asb.showUpd();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSaleBack(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSaleBack(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除销售退货单编号为"' + ids + '"的数据项？').then(function () {
        return delSaleBack(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('xiaoshou/saleBack/export', {
        ...this.queryParams
      }, `saleBack_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
